import __config from '../../../config/env'
const app = getApp()

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    version: app.globalData.version,
    config: app.globalData.config,
    wxUser: null,
    userInfo: null,
    providerInfo: null,
    orderCountAll: []
  },
  onShow() {
    let wxUser = app.globalData.wxUser
    this.setData({
      wxUser: wxUser
    })
    this.userInfoGet()
    this.loadProviderInfo()
    this.orderCountAll()
    app.doLogin()
  },
  onLoad() {
    this.setData({
      theme: app.globalData.theme
    });
  },
  /**
   * 小程序设置
   */
  settings: function () {
    wx.openSetting({
      success: function (res) {
        console.log(res.authSetting)
      }
    })
    // wx.navigateTo({
    //   url: '/pages/user/user-edit/index'
    // })
  },
  onChooseAvatar(e) {
    const that = this
    // 临时路径
    let avatarUrl = e.detail.avatarUrl
    // 上传头像到服务器
    wx.showLoading({
      title: '加载中',
    })
    wx.uploadFile({
      url: __config.basePath + '/mall/api/ma/file/upload',
      filePath: avatarUrl,
      header: {
        'app-id': wx.getAccountInfoSync().miniProgram.appId,
        'third-session': wx.getStorageSync('third_session') != null ? wx.getStorageSync('third_session') : ''
      },
      name: 'file',
      formData: {
        'fileType': 'image',
        'dir': 'userinfo/headimg/'
      },
      success(uploadFileRes) {
        if (uploadFileRes.statusCode == '200') {
          let link = JSON.parse(uploadFileRes.data).link
          app.api.userInfoPut({
            id: that.data.userInfo.id,
            headimgUrl: link
          }).then(res => {
            that.userInfoGet()
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '上传失败：' + uploadFileRes.data,
            success(res) {}
          });
        }
      },
      fail: (err) => {
        console.log(err)
      },
      complete(res) {
        wx.hideLoading()
      }
    })
  },
  onGetNickName(e) {
    let nickName = e.detail.value
    app.api.userInfoPut({
      id: this.data.userInfo.id,
      nickName: nickName
    }).then(res => {
      this.userInfoGet()
    })
  },
  //获取商城用户信息
  userInfoGet() {
    app.api.userInfoGet()
      .then(res => {
        this.setData({
          userInfo: res.data
        })
      })
  },
  // 获取当前师傅信息
  loadProviderInfo() {
    app.api.providerInfoGet().then(res => {
      let data = res.data || {}
      this.setData({
        providerInfo: data
      })
    })
  },
  orderCountAll() {
    app.api.providerOrderCountAll()
      .then(res => {
        this.setData({
          orderCountAll: res.data
        })
      })
  },
  orderTipClick() {
    let orderTip = this.data.orderTip
    if (orderTip) {
      if (orderTip.status == 0) {
        // 去支付
        wx.navigateTo({
          url: '/pages/order/order-detail/index?callPay=true&id=' + orderTip.id
        })
      } else if (orderTip.status == 2) {
        // 导航
        let expressSite = orderTip.expressSite
        if (expressSite) {
          wx.openLocation({
            longitude: Number.parseFloat(expressSite.longitude),
            latitude: Number.parseFloat(expressSite.latitude),
            name: expressSite.name
          })
        }
      }
    }
  },
  countDownDone() {
    this.orderCountAll()
  },
  handleContact(e) {
    wx.makePhoneCall({
      phoneNumber: '400888888'
    })
  }
})